package com.niit.controller;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.niit.pojo.User;
import com.niit.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.niit.utils.ByteArrayToBase64Adapter.GSON;

@RestController
@RequestMapping("/api/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/add")
    public String addUser(User user) {
        JsonObject jsonObject = new JsonObject();
        List<User> users = userService.queryUser(new User(user.getUname()));
        if(users == null || users.size() == 0) {
            int i = userService.addUser(user);
            jsonObject.addProperty("code", i);
        }else {
            jsonObject.addProperty("code", 0);
        }
        return jsonObject.toString();
    }

    @RequestMapping("/login")
    public String login(User user) {
        JsonObject jsonObject = new JsonObject();
        List<User> users = userService.queryUser(user);
        if(users != null && users.size() == 1) {
            jsonObject.addProperty("code", 1);
            jsonObject.add("success", GSON.toJsonTree(users.get(0)).getAsJsonObject());
            System.out.println(users.get(0).toString());
        }else {
            jsonObject.addProperty("code", 0);
        }
        return jsonObject.toString();
    }

    @RequestMapping("/delete")
    public String deleteUser(User user){
        JsonObject jsonObject = new JsonObject();
        List<User> users = null;
        if(user.getUid() == null){
            users = userService.queryUser(new User(user.getUname()));
        }
        if(users != null){
            int i = userService.deleteUser(users.get(0).getUid());
            jsonObject.addProperty("code", i);
        }else {
            jsonObject.addProperty("code", 0);
        }
        return jsonObject.toString();
    }

    @RequestMapping("/update")
    public String updateUser(@RequestBody User user){
        JsonObject jsonObject = new JsonObject();
        int i = userService.updateUser(user);
        jsonObject.addProperty("code", i);
        if(i == 1){
            user = userService.queryUser(user.getUid());
            jsonObject.add("user", new Gson().toJsonTree(user).getAsJsonObject());
        }
        return jsonObject.toString();
    }

    @RequestMapping("/query")
    public String queryUser(User user){
        JsonObject jsonObject = new JsonObject();
        List<User> users = userService.queryUser(user);
        if(users != null && users.size() > 0) {
            jsonObject.addProperty("code", 1);
            jsonObject.add("success", GSON.toJsonTree(users).getAsJsonArray());
        }else {
            jsonObject.addProperty("code", 0);
        }
        return jsonObject.toString();
    }
}
